﻿using System;
using System.Net;
using System.Threading.RateLimiting;
using Common;
using Microsoft.AspNetCore.Http;
using static Org.BouncyCastle.Math.EC.ECCurve;

namespace Extensions.Services.RateLimit.Policys;
/// <summary>
/// 滑动窗口
/// </summary>
public class SlidingWindowLimiterPolicy:BaseLimiterPolicy
{
    protected override RateLimitPartition<IPAddress> RateLimit(IPAddress ipAddress)
    {
        return RateLimitPartition.GetSlidingWindowLimiter(ipAddress, _ => new SlidingWindowRateLimiterOptions()
        {
            // 每次允许的 数量
            PermitLimit = base.Config.PermitLimit,
            // 窗口的时间
            Window = TimeSpan.FromSeconds(base.Config.Window),
            // 划分的窗口的最大段数
            SegmentsPerWindow = base.Config.SegmentsPerWindow,
            //队列处理顺序  
            QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
            // 等待队列的长度
            QueueLimit = base.Config.QueueLimit
        });
    }
}